package heima3_2.cn.osc.ui.activity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;

import com.flipboard.bottomsheet.BottomSheetLayout;


import net.oschina.app.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import heima3_2.cn.osc.base.BaseActivity;
import heima3_2.cn.osc.constant.MainTab;


public class MainActivity extends BaseActivity {
    @Bind(R.id.iv_search)
    ImageView mIvSearch;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.lv_left_menu)
    ListView mLvLeftMenu;
    @Bind(R.id.drawerLayout_home)
    DrawerLayout mDrawerLayoutHome;
    @Bind(R.id.flt_content)
    FrameLayout mFltContent;
    @Bind(R.id.tabhost)
    FragmentTabHost mTabhost;
    @Bind(R.id.iv_quick_option)
    ImageView mIvQuickOption;
    @Bind(R.id.bottomSheetLayout)
    BottomSheetLayout mBottomSheetLayout;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_main;

    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        initToolBar();
        initTab();
    }

    @Override
    public void iniEvent() {
        mIvQuickOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initBottomSheet();
            }
        });

    }


    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {

    }

    View bottomSheet;
    private void initBottomSheet() {
        if (bottomSheet == null) {
            bottomSheet = LayoutInflater.from(this).inflate(R.layout.bottom_sheet, (ViewGroup) getWindow().getDecorView(),false);
        }
        mBottomSheetLayout.showWithSheetView(bottomSheet);
        ImageView ivShowBottom = (ImageView) bottomSheet.findViewById(R.id.iv_show_or_hide);
        ivShowBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBottomSheetLayout.isSheetShowing()) {
                    mBottomSheetLayout.dismissSheet();
                }
            }
        });
    }

    /**
     * 初始化ToolBar，目前实现了左上角的点击开闭侧滑菜单
     */
    private void initToolBar() {
        mToolbar.setNavigationIcon(R.drawable.actionbar_back_icon_normal);
        // 设置title和subTitle
//        mToolbar.setTitle(R.string.app_name);
        mToolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        setSupportActionBar(mToolbar);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        final DrawerLayout drawerLayout
                = (DrawerLayout) findViewById(R.id.drawerLayout_home);

        //该方法会自动和actionBar关联
        final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        toggle.syncState();
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                toggle.onDrawerSlide(drawerView, slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                toggle.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                toggle.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                toggle.onDrawerStateChanged(newState);
            }
        });
    }

    /**
     * 初始化了底部的FragmentTabHost
     */
    private void initTab() {
        //将FragmentTabHost于防止fragment的容器frameLayout结合
        mTabhost.setup(this, getSupportFragmentManager(), R.id.flt_content);

        MainTab[] tabs = MainTab.values();
        final int size = tabs.length;
        for (int i = 0; i < size; i++) {
            // 找到每一个枚举的Fragment对象
            MainTab mainTab = tabs[i];
            //fragment
            // 1. 创建一个新的选项卡
            TabHost.TabSpec tab = mTabhost.newTabSpec(this.getResources().getString(mainTab.getResName()));

            // ------------------------------------------------- 自定义选项卡 ↓
            View indicator = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.tab_indicator, null);
            TextView title = (TextView) indicator.findViewById(R.id.tab_title);
            Drawable drawable = this.getResources().getDrawable(
                    mainTab.getResIcon());
            //设置TextView上下左右的绘图
            title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                    null);
            if (i == 2) {

                //当是正中间的标签，则隐藏 ，用于显示ImageView 弹出对话框的控件
                indicator.setVisibility(View.INVISIBLE);
            }
            title.setText(getString(mainTab.getResName()));
            //设置指示器，是一个View
            tab.setIndicator(indicator);
            // ------------------------------------------------- 以上 ↑

            Bundle bundle = new Bundle();
            bundle.putString("key",
                    "content: " + getString(mainTab.getResName()));
            // 2. 把新的选项卡添加到TabHost中
            mTabhost.addTab(tab, mainTab.getClz(), bundle);
        }
    }
}
